package com.xbongbong.crm.service.condition;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.abstracts.AbstractConditionService;
import com.xbongbong.parent.pojo.list.ConditionListPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.SalesOpportunityEnum;
import com.xbongbong.saas.model.OpportunityFocusModel;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/06/15 16:39
 */
@Service("opportunityConditionServiceImpl")
public class OpportunityConditionServiceImpl extends AbstractConditionService {
    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private OpportunityFocusModel opportunityFocusModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), XbbRefTypeEnum.ALL_OPPORTUNITY.getCode(), XbbRefTypeEnum.KEY_OPPORTUNITY.getCode());
    }

    @Override
    public List<ConditionsEntityExt> packageConditionBySubBusinessType(ConditionListPojo conditionListPojo) {
        String corpid = conditionListPojo.getCorpid();
        String userId = conditionListPojo.getUserVO().getUserId();
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(conditionListPojo.getSubBusinessType());
        List<ConditionsEntityExt> conditionList = new ArrayList<>();
        if (xbbRefTypeEnum == XbbRefTypeEnum.KEY_OPPORTUNITY) {
            boolean isShare;
            try {
                isShare = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.SHARE_FOCUS_OPPORTUNITY.getAlias(), BasicConstant.IS_USE.toString());
            } catch (XbbException e) {
                isShare = Objects.equals(CompanyConfigEnum.SHARE_FOCUS_OPPORTUNITY.getValue(), BasicConstant.IS_USE.toString());
            }
            List<Long> dataIdList;
            if (isShare) {
                dataIdList = opportunityFocusModel.getShareOpportunityIdList(corpid);
            } else {
                dataIdList = opportunityFocusModel.getNoShareOpportunityIdList(userId, corpid);
            }
            dataIdList.add(-1L);
            ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
            conditionsEntityExt.setAttr(FieldTypeEnum.DATAID.getAlias());
            conditionsEntityExt.setFieldType(FieldTypeEnum.DATAID.getType());
            conditionsEntityExt.setName(FieldTypeEnum.DATAID.getDescription());
            conditionsEntityExt.setSymbol(ConditionEnum.IN.getSymbol());
            conditionsEntityExt.setValue(new ArrayList<>(dataIdList));
            conditionList.add(conditionsEntityExt);
        }
        Integer statusFilter = conditionListPojo.getStatusFilter();
        if (!(Objects.isNull(statusFilter) || statusFilter == 0)){
            ConditionsEntityExt statusFilterEntityExt = new ConditionsEntityExt();
            statusFilterEntityExt.setAttr(SalesOpportunityEnum.IS_ARCHIVED.getAttr());
            statusFilterEntityExt.setFieldType(SalesOpportunityEnum.IS_ARCHIVED.getFieldType());
            statusFilterEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
            statusFilterEntityExt.setValue(Collections.singletonList(statusFilter));
            conditionList.add(statusFilterEntityExt);
        }
        return conditionList;
    }
}
